import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QListWidget, QListWidgetItem, QPushButton, QHBoxLayout, \
    QTableWidget, QSizePolicy, QTableWidgetItem
from PyQt5.QtCore import Qt


class ExamPaperEditor(QWidget):
    def __init__(self):
        super().__init__()

        # self.main_class=QTableWidget(7,3)
        # for row in range(self.main_class.rowCount()):
        #     for col in range(self.main_class.columnCount()):
        #         student = QTableWidgetItem('学员编号:')
        #         grade = QTableWidgetItem('所属年级:')
        #         class_hours = QTableWidgetItem('课时数:')
        #         # 设置文本对齐方式：Qt.AlignCenter 表示水平和垂直居中
        #         self.main_class.setItem(row,col,student)
        #         self.main_class.setItem(row,col,grade)
        #         self.main_class.setItem(row,col,class_hours)
        # lay=QHBoxLayout()
        # lay.setAlignment(Qt.AlignCenter|Qt.AlignHCenter)
        # # 设置 QTableWidget 的大小策略
        # self.main_class.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        # lay.addStretch(1)
        # lay.addWidget(self.main_class)
        # lay.addStretch(1)
        # self.setLayout(lay)
        # self.adjustSize()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('试卷编辑器')

        # 设置主布局
        main_layout = QVBoxLayout()

        # 创建一个 QListWidget 用于显示题目
        self.question_list = QListWidget()
        self.question_list.setDragDropMode(QListWidget.InternalMove)  # 启用内部拖拽

        # 添加一些示例题目
        for i in range(1, 6):
            item = QListWidgetItem(f'题目 {i}')
            self.question_list.addItem(item)

        # 按钮用于添加新的题目
        add_question_button = QPushButton('添加题目')
        add_question_button.clicked.connect(self.add_question)

        # 布局管理
        button_layout = QHBoxLayout()
        button_layout.addWidget(add_question_button)

        main_layout.addWidget(self.question_list)
        main_layout.addLayout(button_layout)

        self.setLayout(main_layout)

    def add_question(self):
        # 添加一个新的题目到列表中
        new_question = f'新题目 {self.question_list.count() + 1}'
        item = QListWidgetItem(new_question)
        self.question_list.addItem(item)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    editor = ExamPaperEditor()
    editor.show()
    sys.exit(app.exec_())